/**
 * 
 */
package com.qiyou.db;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * c3p0 数据库连接
 * 
 * @author 赛艳志
 * 
 */
public class DataBaseConnection implements IDatabase {

    private static final DataBaseConnection dbc = new DataBaseConnection();
    private final ComboPooledDataSource cpds;

    private DataBaseConnection() {
        cpds = new ComboPooledDataSource();
        cpds.setUser("root");
        cpds.setPassword("root");
        cpds.setJdbcUrl("jdbc:mysql://127.0.0.1/7you?autoReconnect=true&useUnicode=true&characterEncoding=utf-8");
        try {
            cpds.setDriverClass("com.mysql.jdbc.Driver");
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        cpds.setInitialPoolSize(20);
        cpds.setMinPoolSize(10);
        cpds.setMaxPoolSize(50);
        cpds.setMaxStatements(50);
        cpds.setMaxIdleTime(60);
    }

    /**
     * 获取数据库实
     * 
     * @return 数据库实
     */
    public static final DataBaseConnection getInstance() {
        return dbc;
    }

    /**
     * 获取数据库连
     * 
     * @return 数据库连
     */
    @Override
    public final Connection getConnection() {
        try {
            return cpds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
